package com.example.common.mvvm.viewmodel;

import android.os.Bundle;
import android.util.Log;

import androidx.lifecycle.ViewModel;

import com.example.common.field.BusAction;
import com.example.common.field.DataActionEntity;
import com.example.common.mvvm.databus.LiveDataBus;
import com.example.common.mvvm.model.BaseRespository;
import com.example.common.utils.ToastUtils;
import com.example.network_sdk.BaseEntity;
import com.example.network_sdk.entity.NetErroEntity;

import org.jetbrains.annotations.NotNull;

import java.lang.reflect.ParameterizedType;
import java.util.HashMap;

import io.reactivex.Observer;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

public abstract class BaseViewModel<M extends BaseRespository> extends ViewModel implements IViewModel, Observer<BaseEntity> {

    protected M respository;
    private  CompositeDisposable compositeDisposable;
    private   UIDataFunction uiDataFunction;

    public UIDataFunction getUiDataFunction() {
        return uiDataFunction;
    }

    public BaseViewModel() {
        uiDataFunction = new UIDataFunction();
        compositeDisposable = new CompositeDisposable();
        initRespository();
    }

    private void initRespository() {
        ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
        Class clazz= (Class) type.getActualTypeArguments()[0];
        try {
            respository= (M) clazz.newInstance();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }

    }

    public abstract void onResult(BaseEntity entity);
    public abstract void initData();

    @Override
    public void onCreate() {
        initData();
    }

    @Override
    public void onDestroy() {
        compositeDisposable.dispose();
        compositeDisposable.clear();
    }

    //显示dialog
    protected void showDialog(){

    }
    //关闭dialog
    protected void hideDialog(){

    }

    //关闭界面
    public void finishPage(){
        DataActionEntity entity = new DataActionEntity();
        entity.action = BusAction.FINISHPAGE;
    }


    @Override
    public void onSubscribe(@NotNull Disposable d) {
        compositeDisposable.add(d);
    }

    @Override
    public void onNext(@NotNull BaseEntity baseEntity) {
        if (baseEntity instanceof NetErroEntity){
            NetErroEntity netErroEntity= (NetErroEntity) baseEntity;
            ToastUtils.showShort(netErroEntity.message);
            return;
        }
        onResult(baseEntity);
    }
    @Override
    public void onError(@NotNull Throwable e) {
        Log.i("onError", "onError: "+e.getMessage());
        ToastUtils.showShort("请求错误");
    }

    @Override
    public void onComplete() {
        hideDialog();
    }
    public static class UIDataFunction{
        private LiveDataBus<DataActionEntity> uiData=new LiveDataBus<>();

        public LiveDataBus<DataActionEntity> getUiData() {
            return uiData;
        }

        public void post(DataActionEntity entity){
            uiData.setValue(entity);
        }
    }

}
